package com.ruoyi.huanjing.controller;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;

@RestController
public class FileManController {

    private static List<String> paiChuList=new ArrayList<>(Arrays.asList( "光学参数", "大气辐射亮度（实测）", "大气成分廓线（实测）"));

    @Anonymous
    @PostMapping("/getfilelist")
    public List<Map<String, Object>> getFiles(@RequestParam(required = false) String path) {
        String searchDir = (path == null || path.isEmpty()) ? "G:\\环境数据\\" : path;
        List<Map<String, Object>> fileInfoList = new ArrayList<>();
        int id = 0;
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(searchDir))) {
            for (Path entry : stream) {
                // 获取当前目录的深度
                int depth = getDirectoryDepth(entry, searchDir);

                // 排除第一级和第六级目录
                if (depth == 6) {
                    continue;
                }
                if (Files.isDirectory(entry)) {
                    Map<String, Object> fileInfo = new HashMap<>();
                    fileInfo.put("id", String.valueOf(id++));
                    fileInfo.put("name", entry.getFileName().toString());
                    fileInfo.put("path", entry.toString());
                    boolean isLeaf = true;
                    // 如果目录在paiChuList中，设置isLeaf为true
                    if (paiChuList.contains(entry.getFileName().toString())) {
                        isLeaf = true;  // 直接设置为true
                    } else {
                        // 否则，检查是否有子目录，决定isLeaf的值
                        try (DirectoryStream<Path> subStream = Files.newDirectoryStream(entry)) {
                            for (Path subEntry : subStream) {
                                if (Files.isDirectory(subEntry)) {
                                    isLeaf = false;  // 如果有子目录，则不是叶子节点
                                    break;
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    fileInfo.put("leaf", isLeaf);
                    fileInfoList.add(fileInfo);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return fileInfoList;
    }

    // 获取文件夹的深度
    private int getDirectoryDepth(Path entry, String rootDir) {
        Path relativePath = Paths.get(rootDir).relativize(entry);
        return relativePath.getNameCount();
    }


    @Anonymous
    @GetMapping("/diskscanner")
    public List<String> diskscanner() {
        // 获取所有磁盘根目录
        try {
            // 获取所有磁盘根目录
            File[] roots = File.listRoots();

            if (roots == null || roots.length == 0) {
                return Collections.emptyList();
            }

            return Arrays.stream(roots)
                    .map(File::getAbsolutePath)
                    .collect(Collectors.toList());
        } catch (SecurityException e) {
            return Collections.emptyList();
        }
    }

}